package com.leslie.ai.controller;

import com.leslie.ai.domain.TestEntity;
import com.leslie.ai.domain.TestEntitys;
import com.leslie.ai.service.TestEntityService;
import com.leslie.common.utils.poi.EasyExcelUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import java.util.ArrayList;

/**
 * @author Leslie Lee
 * @version 2003/04/01
 * @date 1956/09/12
 */
@Tag(name = "AI模块", description = "基于 Ollama 对接 deepseek-r1:1.5b 版本")
@RestController
@RequestMapping("/ai")
@RequiredArgsConstructor
public class AIChatController {

    private final ChatClient chatClient;

    private final TestEntityService testEntityService;

    /**
     * 流式调用
     *
     * @author Leslie Lee
     * @date 1956-09-12
     * @version 2003-04-01
     */
    @Operation(summary = "给 DeepSeek 发送消息-流式", method = "1", parameters = {
            @Parameter(name = "prompt", description = "消息"),
    }
    )
    @GetMapping(value = "/chat", produces = "text/html;charset=utf-8")
    public Flux<String> chatStream(@RequestParam String prompt) {
        return chatClient.prompt().user(prompt).stream().content();
    }

    @Operation(summary = "给 DeepSeek 发送消息-普通", method = "2", parameters = {
            @Parameter(name = "prompt", description = "消息"),
    }
    )
    @GetMapping(value = "/chatStream", produces = "text/html;charset=utf-8")
    public String chat(@RequestParam String prompt) {
        return chatClient.prompt().user(prompt).call().content();
    }

    @Operation(summary = "测试", method = "3")
    @GetMapping("/test")
    @SneakyThrows
    public String test(HttpServletResponse response) {
        ArrayList<TestEntity> list = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            TestEntity testEntity = new TestEntity();
            testEntity.setName("test" + i);
            testEntity.setSex("男" + i);
            list.add(testEntity);
        }
        ArrayList<TestEntity> list2 = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            TestEntity testEntity = new TestEntity();
            testEntity.setName("2test" + i);
            testEntity.setSex("2男" + i);
            list2.add(testEntity);
        }
        EasyExcelUtils.downloadExcel(response, "测试文件", new String[]{"测试1", "测试2"}, new Class[]{TestEntity.class, TestEntitys.class}, list, list2);
        return "哈哈";
    }

    @Operation(summary = "删除测试", method = "4", parameters = {
            @Parameter(name = "id", description = "Id"),
    }
    )
    @GetMapping("/test2/{id}")
    @SneakyThrows
    public String test2(@PathVariable Integer id) {
        int i = testEntityService.deleteById(id);
        if (i > 0)
            return "删了";
        return "没删";
    }

    @Operation(summary = "修改测试", method = "5", parameters = {
            @Parameter(name = "id", description = "Id"),
    }
    )
    @GetMapping("/test3/{id}")
    @SneakyThrows
    public String test3(@PathVariable Integer id) {
        int i = testEntityService.updateById(id);
        if (i > 0)
            return "改了";
        return "没改";
    }

}
